package Exp_1;

public class LinkList {
    public Node head;
    public Node tail;

    public LinkList() {
        head = new Node();
        tail = new Node();
        head.next = tail;
        tail.next = head;
    }
    //链表长度(输出结果)
/*    public void getLength() {
        ws.Node p = head.next;
        int length = 0;
        while (p != tail) {
            p = p.next;
            ++length;
        }
        System.out.println("链表长度是:"+length);
    }*/
/*    //链表长度(有返回值)
    public int length() {
        ws.Node p = head.next;
        int length = 0;
        while (p != tail) {
            p = p.next;
            ++length;
        }
        return length;
    }*/
    //获取某个位置元素的值
    public void get(int i) {
        Node p = head.next;
        int j = 0;
        while (p != tail && j<i) {
            p = p.next;
            ++j;
        }
        if (j>i || p == tail) {
            System.out.println("第"+i+"个元素不存在");
        } else {
            System.out.println("第"+i+"个元素是:"+p.data);
        }
    }
    //查找某个元素所在位置(有返回值)
    public int indexOf(Object data) {
        Node p = head.next;
        int j = 0;
        while (p != tail && !p.data.equals(data)) {
            p = p.next;
            ++j;
        }
        if (p != tail) {
            return j;
        } else {
            return -1;
        }
    }
/*    ////查找某个元素所在位置(无返回值，输出结果)
    public void getIndexOf(Object data) {
        ws.Node p = head.next;
        int j = 0;
        while (p != tail && !p.data.equals(data)) {
            p = p.next;
            ++j;
        }
        if (p != tail) {
            System.out.println("该元素在第"+j+"个位置");
        } else {
            System.out.println("没有找到该元素");
        }
    }*/
    //某个位置插入某个元素
    public void insert(int i,Object data) {
        Node p = head;
        int j = -1;
        while (p != tail && j<i-1) {
            p = p.next;
            ++j;
        }
        if (j>i-1 || p == tail) {
            System.out.println("插入位置不合理");
        }
        Node s = new Node(data);
        if (i == 0) {
            s.next = head.next;
            head.next = s;
        } else {
            s.next = p.next;
            p.next = s;
        }
    }
    //删除某个位置的元素
    public void delete(int i) {
        Node p = head;
        int j = -1;
        while (p != tail && j<i-1) {
            p = p.next;
            ++j;
        }
        if (j>i-1 || p == tail) {
            System.out.println("删除位置不合法");
        }
        p.next = p.next.next;
    }
    //显示链表元素
    public void display() {
        Node p = head.next;
        System.out.print("链表各元素为:");
        while (p != tail) {
            System.out.print(p.data+" ");
            p = p.next;
        }
        System.out.println();
    }
    //获取链表最小值并放在链表首位
    public void setMinDataAhead() {
        Node p = head.next;
        int minData = (Integer)(p.data);
        while (p != tail) {
            if (minData > (Integer)(p.data)) {
                minData = (Integer)(p.data);
            }
            p = p.next;
        }
        delete(indexOf(minData));
        insert(0,minData);
    }
}